package org.freedoit.server.dao;

// Generated Oct 11, 2012 3:54:06 PM by Hibernate Tools 3.4.0.CR1

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import org.freedoit.server.om.User;
import org.freedoit.shared.exception.ServerException;
import org.freedoit.shared.util.Constants;

/**
 * Home object for domain model class User.
 * @see org.freedoit.om.User
 * @author Hibernate Tools
 */
public class UserDAO {

//	private static final Logger log = Logger.getLogger(UserDAO.class.getName());

	private final PersistenceManagerFactory pmFactory = DAOFactory.getPersistenceManagerFactory();

	public void persist(User user) {
		////log.debug("persisting User instance");
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			pm.makePersistent(user);
			////log.debug("persist successful");
		} catch (RuntimeException re) {
			////log.error("persist failed", re);
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}

	public void delete(User taskType) {
		////log.debug("deleting User instance");
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			pm.currentTransaction().begin();
			// We don't have a reference to the selected Product.
			// So we have to look it up first,
			taskType = pm.getObjectById(User.class, taskType.getUserId());
			pm.deletePersistent(taskType);

			pm.currentTransaction().commit();
			//////log.debug("delete successful");
		} catch (RuntimeException re) {
			//////log.error("delete failed", re);
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}

	public void update(User user) {
		//////log.debug("updating User instance");
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			pm.currentTransaction().begin();
			pm.makePersistent(user);
			pm.currentTransaction().commit();
			//////log.debug("update successful");
		} catch (RuntimeException re) {
			//////log.severe("update failed\n" + re.getMessage());
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}

	public User findById(Long id) {
		////log.debug("getting User instance with id: " + id);
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			User instance = pm.getObjectById(User.class, id);
			if (instance == null) {
				////log.debug("get successful, no instance found");
			} else {
				////log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			////log.error("get failed", re);
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public User findByUserName(String userName) {
		////log.debug("finding User instances by user name:" + userName);
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("select from ").append(User.class.getName());
			sbQuery.append(" where userName == '").append(userName).append("'");
			sbQuery.append(" && status == ").append(Constants.ACTIVE_INTEGER);
			List<User> results = (List<User>) pm.newQuery(sbQuery.toString()).execute();
			
			int iSize = 0;
			if (results != null) {
				iSize = results.size();
			}
			////log.debug("find by user name successful, result size: " + iSize);
			
			if (iSize > 0) {
				return results.get(0);
			} else {
				return null;
			}
		} catch (RuntimeException re) {
			////log.error("find by user name failed", re);
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}
}
